package com.wzx.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;
import com.wzx.basics.utils.PageInfo;
import com.wzx.basics.utils.ResultUtil;

import com.wzx.data.entity.Subject;
import com.wzx.data.service.ISubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wzx.data.vo.SubjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *     科目表 前端控制器
 * </p>
 * @author wzx
 */
@Slf4j
@RestController
@Api(tags = "科目管理接口")
@RequestMapping("/wzx/subject")
public class SubjectController {

    @Autowired
    private ISubjectService iSubjectService;


    @ApiOperation("科目分类列表")
    @GetMapping("/getChildSubject")
    public Result getChildSubject(String id) {
        List<Subject> list = iSubjectService.selectSubjectList(id);
        return ResultUtil.data(list);
    }

    @PostMapping("/importTemplate")
    @ApiOperation("下载模板")
    public void importTemplate(HttpServletResponse response) {
        iSubjectService.importTemplate(response);
    }
    @ApiOperation("科目分类导出")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response) {
        iSubjectService.exportData(response);
    }


    @ApiOperation("科目分类导入")
    @PostMapping("/importData")
    public Result importData(MultipartFile file) {
        iSubjectService.importData(file);
        return ResultUtil.success();
    }
    @ApiOperation("新增")
    @PostMapping("/save")
    public Result save(Subject subject){
       iSubjectService.save(subject);
       return ResultUtil.success();
   }

    @ApiOperation("删除")
    @DeleteMapping("/deleteById")
    public Result delete(@RequestParam String[] ids){
        try{
            for(String id:ids){
                iSubjectService.deleteSubject(id);
            }
        }catch (Exception e){
            return ResultUtil.error(e.getMessage());
        }
       return ResultUtil.success();
   }
    @ApiOperation("根据Id获取")
    @GetMapping("/getById")
    public Result<Subject> getById(@RequestParam String id){
        return ResultUtil.data(iSubjectService.getById(id));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public Result update(Subject subject){
        iSubjectService.updateById(subject);
        return ResultUtil.success();
    }
    @ApiOperation("查询列表")
    @GetMapping("/list")
    public Result<List<Subject>> list(@ModelAttribute SubjectVo subjectVo, @ModelAttribute PageVo pageVo){
        final LambdaQueryWrapper<Subject> lambda = new QueryWrapper<Subject>().lambda();
        this.buildCondition(lambda,subjectVo);
        return ResultUtil.data(iSubjectService.list(lambda));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result page(SubjectVo vo){
        final LambdaQueryWrapper<Subject> lambda = new QueryWrapper<Subject>().lambda();
        this.buildCondition(lambda,vo);
        final IPage<Subject> page = iSubjectService.page(new Page<>(vo.getPageNumber(), vo.getPageSize()), lambda);
        PageInfo<Subject> pageInfo = new PageInfo<>(vo.getPageNumber(), vo.getPageSize());
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setTotalList(page.getRecords());
        return ResultUtil.data(pageInfo);
    }
    @ApiOperation(value = "搜索菜单")
    @GetMapping("/search")
    public Result<List<Subject>> searchSubjectList(@RequestParam String title){
        QueryWrapper<Subject> qw = new QueryWrapper<>();
        qw.like("name",title);
        qw.orderByAsc("sort");
        return new ResultUtil<List<Subject>>().setData(iSubjectService.list(qw));
    }
    @GetMapping("/getAllList")
    @ApiOperation(value = "查询全部菜单")
    public Result<List<Subject>> getAllList(){
        // 顶级科目列表
        List<Subject> list0 = iSubjectService.getSubjectListByParentId("0");
        for(Subject p0 : list0){
            // 一级
            List<Subject> list1 = iSubjectService.getSubjectListByParentId(p0.getId());
            p0.setChildren(list1);
        }
        return new ResultUtil<List<Subject>>().setData(list0);
    }
    @GetMapping("/getName")
    @ApiOperation(value = "查询主科目名")
    public Result getName(){
        // 顶级科目列表
        List<Subject> list = iSubjectService.getSubjectListByParentId("0");
        List<String> names = new ArrayList<String>();
        for(Subject subject : list){
           names.add(subject.getName());
        }
        return new ResultUtil().setData(names);
    }
    @GetMapping("/getSubject")
    @ApiOperation(value = "获取二级科目")
    public Result getSubject(){
        LambdaQueryWrapper<Subject> lambda = new LambdaQueryWrapper<Subject>();
        lambda.ne(Subject::getParentId,"0");
        List<Subject> list = iSubjectService.list(lambda);
        return ResultUtil.data(list);
    }

    /**
     * 构建查询条件
     * @param lambda
     * @param vo
     */
    private void buildCondition(LambdaQueryWrapper<Subject> lambda, SubjectVo vo) {
        if (ObjectUtils.isNotEmpty(vo.getName())){
            lambda.like(Subject::getName,vo.getName());
        }
        //根据startTime
        if (ObjectUtils.isNotEmpty(vo.getStartTime())){
            lambda.ge(Subject::getCreateTime, vo.getStartTime());
        }
        //根据endTime
        if (ObjectUtils.isNotEmpty(vo.getEndTime())){
            lambda.le(Subject::getCreateTime, vo.getEndTime());
        }
        lambda.eq(Subject::getDelFlag,0);
        lambda.orderByDesc(Subject::getCreateTime);
    }



}
